package com.samoiloff.ui.scroll.resize.controller {
	import com.samoiloff.framework.controller.AbstractController;
	import com.samoiloff.ui.scroll.resize.model.ResizeScrollModel;
	import com.samoiloff.ui.scroll.resize.view.ResizeScrollView;

	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author Samoiloff
	 */
	public class ResizeScrollViewController extends BaseResizeScrollController {
		
		private static const MAX_SCROLL_COUNT:int = 10;
		
		private var _scrollModel: ResizeScrollModel;		
		private var _scrollView: ResizeScrollView;
		
		private var _root	: DisplayObject;
		
		private var _mouseY	: Number;
		
		private var _startDragScrollPosition:Number;
		
		private var _scrollPosition	: Number;
		private var _scrollCount	: int;
		
		public function ResizeScrollViewController(model: ResizeScrollModel) {
			super(model);
			
			_scrollModel = model;
			_scrollView = model.view; 
			
			_scrollPosition = 0;
			
			_scrollView.slider.mc.addEventListener(MouseEvent.MOUSE_DOWN, onSliderMouseDown);
			
			_scrollView.upArrow.mc.addEventListener(MouseEvent.MOUSE_DOWN, onUpMouseDown);
			
			_scrollView.downArrow.mc.addEventListener(MouseEvent.MOUSE_DOWN, onDownMouseDown);
			
			_scrollView.bg.addEventListener(MouseEvent.MOUSE_DOWN, onBgMouseDown);
		}
		/*
		 * BG
		 */
		private function onBgMouseDown(e:MouseEvent): void {
			var y:Number = _scrollModel.mainMc.mouseY - _scrollView.upArrow.mc.height - _scrollView.slider.mc.height/2;
			_model.scrollPosition = y / _scrollView.vmKoeff;
			onSliderMouseDown();
		}
		/*
		 * DOWN
		 */
		private function onDownMouseDown(event: MouseEvent): void {
			trySetRoot();
			_model.scrollPosition = _scrollPosition;
			_scrollCount = 0;
			
			_root.addEventListener(Event.ENTER_FRAME, onDownEnterFrame);
			_root.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		}
		private function onDownEnterFrame(e:Event = null): void {
			if (_scrollCount > MAX_SCROLL_COUNT || _scrollCount == 0) {
				_scrollPosition = _scrollPosition + _scrollModel.lineScrollSize;
				_model.animateScroll(_scrollPosition);
			}
			_scrollCount++;
			
		}
		/*
		 * UP
		 */
		private function onUpMouseDown(event: MouseEvent): void {
			trySetRoot();
			_model.scrollPosition = _scrollPosition;
			_scrollCount = 0;
			
			_root.addEventListener(Event.ENTER_FRAME, onUpEnterFrame);
			_root.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			
			onUpEnterFrame();
		}
		private function onUpEnterFrame(e:Event = null): void {
			if (_scrollCount > MAX_SCROLL_COUNT || _scrollCount == 0) {
				_scrollPosition = _scrollPosition - _scrollModel.lineScrollSize;
				_model.animateScroll(_scrollPosition);
			}
			_scrollCount++;
		}

		private function onSliderMouseDown(event: MouseEvent = null): void {
			trySetRoot();
			_mouseY = _model.mainMc.mouseY;
			_scrollPosition = _startDragScrollPosition = _model.scrollPosition;
			
			_root.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			_root.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		}

		private function onStageMouseUp(event: MouseEvent): void {
			//onEnterFrame();
			_root.removeEventListener(Event.ENTER_FRAME, onDownEnterFrame);
			_root.removeEventListener(Event.ENTER_FRAME, onUpEnterFrame);
			_root.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			_root.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		}

		private function onEnterFrame(event: Event = null): void {
			var dy:Number = _mouseY - _model.mainMc.mouseY;
			//_mouseY = _model.mainMc.mouseY;
			_scrollPosition = _startDragScrollPosition - ( dy / _scrollView.vmKoeff );
			
			_model.animateScroll(_scrollPosition);
		}
		
		private function trySetRoot():void {
			if (!_root) {
				_root = _model.mainMc.root;
			}
		}

	}
}
